<?php
/**
 * Users Model
 * This model allows for the addition of Users to the system.
 */

 class Account_Model_Users extends Zend_Db_Table_Abstract {
 	
	protected $_name = 'Users';
	
	/*
	 * This Registers the User with the System
	 */
	public function addUser($email, $school, $uname, $password ){
		
		$data = array (
		  'email' => $email,
		  'school' => $school,
		  'username' => $uname,  
		  'password' => hash(md5, $password),
		  'created_at'=> new Zend_Db_Expr("now()")
	);
	$this->insert($data);
	
		}
	
	
	/*
	 * Function: getUser
	 * This returns a specific user when given an id
	 */
	public function getUser($id){
		
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		return $row->toArray();
	}
	
	/*
	 * Function: deleteUser
	 * This deletes a specific user of the system
	 */
	public function deleteUser($id){
		
		$this->delete('id = ' .(int)$id );
	}
	
	/**
	 * Function: GetUserRole
	 * This returns the role of a user within the sytem.
	 */
	public function getUserRole($id){
		
		
	}
	
	/**
	 * Function: GetAllUsers
	 * @return This returns all the Users within the system.
	 */
	public function getAllUsers(){
		
		return $this->fetchAll();
		
	}
	
	/**
	 * Function: editAccount
	 * This function allows a user edit their specific account.
	 */
	public function editAccount($id, $fname, $lname, $email, $school, $uname, $password ){
		
		$data = array (
		  'email' => $email,
		  'school' => $school,
		  'username' => $uname,  
		  'password' => hash('md5', $password),
		  'created_at'=> new Zend_Db_Expr("now()")
	);
	$this->update($data, 'id ='. (int)$id);
	
	}
	
	/**
	 * Function: activateAccount
	 * @param $id
	 * This method activates a users account
	 */
	public function activateAccount($id){
		
		$data = array (
		 'status' => 'active'
		);
		$this->update($data, 'id ='.$id);
	}
	
	public function checkEmail($email){
		
		$email = $this->_db->quote($email);
		$select = new Zend_Db_Select($this->_db);
		$result = $select->from(array('users'), array('val' =>'count(id)')) 
						->where('email ='.$email);
		$ff = $this->_db->fetchAll($result);
		return $ff;
		Zend_Debug::dump($ff);
	}
	/**
	 * Function: sendUserPassword
	 * @param unknown_type $email
	 * This function resets a user's username and password values 
	 */
	public function sendUserPassword($email){
		
		$email = $this->_db->quote($email);
		$user = 'user'.rand(231, 12312);
		$password = 'password'.rand(900, 400000);
		$data = array(
		'username'=> $user,
		'password'=>hash('md5',$password)
		);
		$this->update($data, 'email='.$email);
		
		$details = array();
		//Push the random username and password to an array to be returned to the user.
		array_push($details, $user);
		array_push($details, $password);
		return $details;
	}
	public function returnlastid(){
		
		return $this->getAdapter()->lastInsertId();
	}
 }
